package com.baidu.acg.industry.bigdata.controller;

import com.baidu.acg.industry.bigdata.common.CommonResponse;
import com.baidu.acg.industry.bigdata.model.MinioRequestTxt;
import com.baidu.acg.industry.bigdata.model.ModelTrainRequest;
import com.baidu.acg.industry.bigdata.model.ReasonModelRequest;
import com.baidu.acg.industry.bigdata.model.RenderRequest;
import com.baidu.acg.industry.bigdata.service.YongJiModelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@RequestMapping("/v1/platform/yongji")
@Slf4j
public class YongJiPlatFormController {

    @Autowired
    private YongJiModelService yongJiModelService;

//    @PostMapping("/data/pre")
//    public void preDealFile(@RequestParam("dataSetName") String dataSetName,
//                            @RequestParam("dataCollectPath") String dataCollectPath) {
////        platformService.dataPre(dataSetName,dataCollectPath);
//    }

    /**
     * 模型训练
     */
    @PostMapping("/train/start")
    public String train(@RequestBody ModelTrainRequest modelTrainRequest) {
        return yongJiModelService.runModel(modelTrainRequest);
    }

    @PostMapping("/generateTxt")
    public Integer generateTxt(@RequestBody MinioRequestTxt minioRequestTxt) {
        return yongJiModelService.generateTxtNotDownload(minioRequestTxt);
    }


    /**
     * 模型训练进度
     */
    @GetMapping("/train/process")
    public String getTrainingProgress(@RequestParam("modelTaskId") String modelTaskId) {
        return yongJiModelService.getCombinedProgress(modelTaskId);
    }

    /**
     * 模型训练日志
     */
    @GetMapping("/train/log")
    public String getTrainLog(@RequestParam("modelTaskId") String modelTaskId) {
//        return yongJiModelService.getTrainLog(modelTaskId);
        return yongJiModelService.getTrainLogByMinIo(modelTaskId);
    }

    /**
     * 训练数据查询接口
         */
        @GetMapping("/train/list")
    public Map<String, String> getTrainData(@RequestParam(name = "modelTaskId") String modelTaskId) {
        return yongJiModelService.getTrainList(modelTaskId);
    }

    /**
     * 模型训练3D云图展示接口
     */
    @GetMapping("/train/vtp")
    public List<String> getVtp(@RequestParam("modelTaskId") String modelTaskId) {
        return yongJiModelService.getModelFileList(modelTaskId);
    }

//    @GetMapping("/removeContainer")
//    public int removeContainer(@RequestParam("modelTaskId") String modelTaskId) {
//        return yongJiModelService.removeContainerById(modelTaskId);
//    }

    /**
     * 模型推理接口
     */
    @PostMapping("/reason/model")
    public String reasonModel(@RequestBody ReasonModelRequest request) {
        return yongJiModelService.reasonModel(request);
    }

    /**
     * 推理进度接口
     */
    @GetMapping("/reason/process")
    public String getReasonProgress(@RequestParam("modelTaskId") String modelTaskId) {
        return yongJiModelService.getCombinedProgress(modelTaskId);
    }

    /**
     * 模型推理接口数据查询
     */
    @GetMapping("/reason/model/data")
    public Map<String, String> getReasonModelData(@RequestParam("modelTaskId") String modelTaskId) {
        return yongJiModelService.getReasonModelDataPath(modelTaskId);
    }

    /**
     * 设计优化推理接口数据查询
     */
    @GetMapping("/optimize/model/data")
    public Map<String, String> getOptimizeModelData(@RequestParam("modelTaskId") String modelTaskId) {
        return yongJiModelService.getOptimizeModelData(modelTaskId);
    }


    @PostMapping("/render")
    public String render(@RequestBody RenderRequest request) {
        return yongJiModelService.render(request);
    }
    /**
     * 模型推理3D云图展示接口
     */
    @GetMapping("/reason/vtp")
    public List<String> getCloudPaths(@RequestParam("modelTaskId") String modelTaskId) {
        return yongJiModelService.getReasonFileList(modelTaskId);
    }

    /**
     * 查询推理日志
     */
    @GetMapping("/reason/log")
    public String getReasonLog(@RequestParam("modelTaskId") String modelTaskId) {
        return yongJiModelService.getTrainLogByMinIo(modelTaskId);
    }
    @GetMapping("/removeContainerById")
    public int removeContainerById(@RequestParam("modelTaskId") String modelTaskId) {
        return yongJiModelService.removeContainerById(modelTaskId);
    }

    /**
     * 任务状态查询接口
     */
    @GetMapping("/get/model/status")
    public Map<String, String> getModelStatus(@RequestParam("modelTaskId") String modelTaskId) {
        Map<String, String> result = new HashMap<>();
        // 获取容器状态
        try {
            String containerStatus = yongJiModelService.getContainerStatus(modelTaskId);
            result.put("containerStatus", containerStatus);
        } catch (Exception e) {
            result.put("containerStatus", "Error: " + e.getMessage());
        }
        // 获取进度
        try {
            String progress = yongJiModelService.getCombinedProgress(modelTaskId);
            result.put("progress", progress);
        } catch (Exception e) {
            result.put("progress", "Error: " + e.getMessage());
        }
        return result;
    }

    /**
     * 终止模型训练接口
     */
    @GetMapping("/stop/model")
    public String stopModel(@RequestParam("modelTaskId") String modelTaskId) {
        return yongJiModelService.stopModel(modelTaskId);
    }

    /**
     * 删除minio训练/推理结果集接口
     */
    @GetMapping("/train/result")
    public ResponseEntity<String> deleteTrainResult(@RequestParam("modelTaskId") String modelTaskId) {
        return yongJiModelService.deleteTrainResult(modelTaskId);
    }
    /**
     * 删除render vtp temp文件
     */
    @GetMapping("/render/delete")
    public ResponseEntity<String> deleteRenderResult(@RequestParam("id") Long id) {
        return yongJiModelService.deleteRederResult(id);
    }
}
